<?php

namespace app\lijunxuan\controller;

use app\common\LijunxuanController;
use app\lijunxuan\model\Toollabel as ToollabelDB;
class Toollabel extends LijunxuanController
{
    public function initialize()
    {
        $this->model = new ToollabelDB();
    }

    public function list()
    {
        $list = $this->model
            ->select()
            ->hidden(['create_by', 'update_by']);
        $res = [
            'rows' => $list,
            'total' => count($list)
        ];
        return success('获取成功', $res);
    }

    public function getTool()
    {
        $id = request()->get('id');
        $where = !empty($id) ? [['id', 'IN', $id]] : '';
        $res = $this->model->where($where)->with([
            'tool' => function ($query) {
                $query->hidden(['create_by', 'update_by', 'pivot']);
            }
        ])->select()->hidden(['create_by', 'update_by']);
        $toolList = [];
        foreach ($res as $item) {
            $toolList = array_merge($toolList, $item->tool->toArray());
        }
        $res = array_unique($toolList, SORT_REGULAR);
        array_multisort(array_column($res, 'toolSort'), SORT_DESC, $res);
        return success('获取成功', $res);
    }
}